import 'package:flutter_test/flutter_test.dart';
import 'package:flowplan_app/core/offline/offline_data_manager.dart';
import 'package:flowplan_app/data/models/note_model.dart' as note_model;
import 'package:flowplan_app/data/models/project_model.dart' as project_model;
import 'package:flowplan_app/data/models/todo_model.dart' as todo_model;

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  group('离线功能综合测试', () {
    late OfflineDataManager offlineDataManager;

    setUp(() {
      offlineDataManager = OfflineDataManager();
    });

    tearDown(() async {
      await offlineDataManager.clearAllOperations();
    });

    test('离线数据管理器实例化测试', () {
      expect(offlineDataManager, isNotNull);
    });

    test('创建笔记离线操作', () async {
      final request = note_model.NoteCreateRequest(
        title: '测试笔记',
        content: '这是测试内容',
        noteType: note_model.NoteType.general,
        tags: ['测试'],
        authorId: 1,
        isPublic: true,
      );

      await offlineDataManager.createNoteOfflineOperation(request);

      final operations =
          await offlineDataManager.getOfflineOperationsByType('note');
      expect(operations, isNotEmpty);
      expect(operations.length, 1);
      expect(operations[0].operationType, OfflineOperationType.create);
      expect(operations[0].data['title'], '测试笔记');
    });

    test('创建项目离线操作', () async {
      final request = project_model.ProjectCreateRequest(
        projectName: '测试项目',
        description: '这是测试项目描述',
        priority: project_model.Priority.high,
      );

      await offlineDataManager.createProjectOfflineOperation(request);

      final operations =
          await offlineDataManager.getOfflineOperationsByType('project');
      expect(operations, isNotEmpty);
      expect(operations.length, 1);
      expect(operations[0].operationType, OfflineOperationType.create);
      expect(operations[0].data['projectName'], '测试项目');
    });

    test('创建待办离线操作', () async {
      final request = todo_model.TodoCreateRequest(
        projectId: 1,
        title: '测试任务',
        todoType: todo_model.TodoType.develop,
        priority: todo_model.Priority.high,
      );

      await offlineDataManager.createTodoOfflineOperation(request);

      final operations =
          await offlineDataManager.getOfflineOperationsByType('todo');
      expect(operations, isNotEmpty);
      expect(operations.length, 1);
      expect(operations[0].operationType, OfflineOperationType.create);
      expect(operations[0].data['title'], '测试任务');
    });

    test('离线操作记录的保存和检索', () async {
      // 创建多个不同类型的操作
      final noteRequest = note_model.NoteCreateRequest(
        title: '笔记1',
        content: '内容1',
        noteType: note_model.NoteType.general,
        tags: [],
        authorId: 1,
        isPublic: true,
      );

      final projectRequest = project_model.ProjectCreateRequest(
        projectName: '项目1',
        description: '描述1',
        priority: project_model.Priority.medium,
      );

      await offlineDataManager.createNoteOfflineOperation(noteRequest);
      await offlineDataManager.createProjectOfflineOperation(projectRequest);

      // 检索所有操作
      final allOperations = await offlineDataManager.getOfflineOperations();
      expect(allOperations.length, 2);

      // 按类型检索操作
      final noteOperations =
          await offlineDataManager.getOfflineOperationsByType('note');
      final projectOperations =
          await offlineDataManager.getOfflineOperationsByType('project');

      expect(noteOperations.length, 1);
      expect(projectOperations.length, 1);
    });
  });
}
